
//添加红包
$('#addRedPack').on('click',function (){
    $("#redcarList").empty(); //清空子节点
    document.getElementById("showred").style.display="none";
    if($('input:checkbox:checked').length > 0){
        var carId = new Array();
        var carStatus = 0;

        obj = $("input[type='checkbox'][name='carId']:checked");
        $.each(obj,function (index,item){
            carId.push(item.value);
        })
        if(carStatus > 0){
            layer.msg("请将车辆上线再进行操作！");
            return false;
        }else{
            $.ajax({
                type:"post",
                url : "searchRedPack",
                data :{
                    "carIds" : carId
                },
                dataType:"json",
                success:function(data){
                    if(data.status == "SUCCESS"){
                        if(data.mapList.length>0){
                            document.getElementById("showred").style.display="block";
                            var car = data.mapList;
                            for(var i = 0 ;  i < data.mapList.length; i++){
                                var str =  "<tr><td>" + car[i].carplate + "</td>"
                                    +"<td>" + car[i].price + "</td>"
                                    +"<td>" + car[i].mprice + "</td></tr>";
                                $("#redcarList").append(str);
                            }
                            layui.form.render();
                        }
                    }
                    layer.open({
                        type : 1,
                        title : "设置红包金额",
                        area : ["550px","500px"],
                        content : $("#tan_06"),
                        btn : ["确定","取消"],
                        yes : function (index,layero){
                            //开始遮罩
                            var newCarLoading;
                            newCarLoading = layer.load(2, {
                                shade : [ 0.3, '#000' ]
                            });
                            if($("#redpacketPrice").val() == null || $("#redpacketPrice").val() == ""){
                                layer.msg("红包金额不能为空！");
                                layer.close(newCarLoading);
                                return false;
                            }
                            var reg = /^[0-9]+?[0-9]*$/;  //数字验证
                            if (!reg.test($("#redpacketPrice").val())) {
                                layer.msg("红包金额为正整数！");
                                layer.close(newCarLoading);
                                return false;
                            }
                            if($("#redpacketMinPrice").val() == null || $("#redpacketMinPrice").val() == ""){
                                layer.msg("最低消费金额不能为空！");
                                layer.close(newCarLoading);
                                return false;
                            }
                            if (!reg.test($("#redpacketMinPrice").val())) {
                                layer.msg("最低消费金额为正整数！");
                                layer.close(newCarLoading);
                                return false;
                            }

                            $.ajax({
                                type:"post",
                                url : "addRedPack",
                                data :{
                                    "carIds" : carId,
                                    "price" : $("#redpacketPrice").val(),
                                    "minPrice" : $("#redpacketMinPrice").val(),
                                },
                                dataType:"json",
                                success:function(data){
                                    layer.msg(data.message);
                                    window.location.reload();
                                },
                            })
                        },
                        end : function(){
                            document.getElementById("redParamForm").reset();
                            $("#redpacketPrice").val("");
                            $("#redpacketId").val("");
                        }
                    });
                },
            })
        }
    }else{
        layer.msg("请选择车辆！");
        return false;
    }
});


$('#batch_addRedPack').on('click',function (){
    layer.open({
        type : 1,
        title : "设置红包金额",
        area : ["550px","500px"],
        content : $("#tan_06_batch"),
        btn : ["确定","取消"],
        yes : function (index,layero){
            //开始遮罩
            var newCarLoading;
            newCarLoading = layer.load(2, {
                shade : [ 0.3, '#000' ]
            });

            if ($("#citycode_batch").val() == null ||$("#citycode_batch").val()  == "" ){
                layer.msg("请选择城市！");
                layer.close(newCarLoading);
                return false;
            }

            if ($("#red_type").val() == null ||$("#red_type").val()  == "" ){
                layer.msg("请选择红包类型！");
                layer.close(newCarLoading);
                return false;
            }

            if ($("#red_time").val() == null ||$("#red_time").val()  == "" ){
                layer.msg("请选择生效时间！");
                layer.close(newCarLoading);
                return false;
            }
            if ($("#red_brand").val() == null ||$("#red_brand").val()  == "" ){
                layer.msg("请选择车系！");
                layer.close(newCarLoading);
                return false;
            }

            if($("#batch_redpacketPrice").val() == null || $("#batch_redpacketPrice").val() == ""){
                layer.msg("红包金额不能为空！");
                layer.close(newCarLoading);
                return false;
            }
            var reg = /^[0-9]+?[0-9]*$/;  //数字验证
            if (!reg.test($("#batch_redpacketPrice").val())) {
                layer.msg("红包金额为正整数！");
                layer.close(newCarLoading);
                return false;
            }
            if($("#batch_redpacketMinPrice").val() == null || $("#batch_redpacketMinPrice").val() == ""){
                layer.msg("最低消费金额不能为空！");
                layer.close(newCarLoading);
                return false;
            }
            if (!reg.test($("#batch_redpacketMinPrice").val())) {
                layer.msg("最低消费金额为正整数！");
                layer.close(newCarLoading);
                return false;
            }

            $.ajax({
                type:"post",
                url : "batchAddRedPack",
                data :{
                    "cityCode" : $("#citycode_batch").val(),
                    "brand" : $("#red_brand").val(),
                    "trademark" : $("#red_trademark").val(),
                    "price" : $("#batch_redpacketPrice").val(),
                    "minPrice" : $("#batch_redpacketMinPrice").val(),
                    "redTime" : $("#red_time").val(),
                    "type" : $("#red_type").val(),
                },
                dataType:"json",
                success:function(data){
                    layer.msg(data.msg,{time: 2500},function () {
                        window.location.reload();
                    });

                },
            })
        },
        end : function(){
            document.getElementById("batch_redParamForm").reset();
            $("#redpacketPrice").val("");
            $("#redpacketId").val("");
        }
    });
});